package com.nwpu.intimacy.scan;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.json.JSONObject;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.TextView;

import com.nwpu.intimacy.R;

public class BrowerActivity extends Activity {
	private WebView webView;
	private TextView text;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_brower);
		webView = (WebView) findViewById(R.id.showSite);
		text = (TextView) findViewById(R.id.showText);
		Intent intent = getIntent();
		Bundle bundle = intent.getExtras();
		String s = bundle.getString("result");
		JSONObject json = null;
		String value = null;
		try {
			json = analyResult(s);
			if (json.has("website")) {
				// ����֧��javascript
				webView.getSettings().setJavaScriptEnabled(true);
				// ��������
				webView.getSettings().setAppCacheEnabled(true);
				// ���û���ģʽ
				webView.getSettings().setCacheMode(WebSettings.LOAD_DEFAULT);
				webView.loadUrl(json.getString("website"));
				// �ڵ�ǰ�����������Ӧ
				webView.setWebViewClient(new WebViewClient());
				webView.setVisibility(View.VISIBLE);
				System.out.println("����һ����ַ��ͼƬ���ļ�");
			}
			if (json.has("string")) {
				text.setText(json.getString("string"));
				System.out.println("����һ����ͨ�ַ�");
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private JSONObject analyResult(String s) throws Exception {
		JSONObject json = new JSONObject();
		// �ж��Ƿ���һ����ַ
		Pattern pattern = Pattern
				.compile("(http://|ftp://|https://|www){0,1}[^\u4e00-\u9fa5\\s]*?\\.(com|net|cn|me|tw|fr)[^\u4e00-\u9fa5\\s]*");
		// �ո����
		Matcher matcher = pattern.matcher(s);
		while (matcher.find()) {
			json.put("website", matcher.group(0));
			return json;
		}
		// �ж��ǲ���һ��ͼƬ�����ı�������,�򿪷�ʽͬwebsite
		if (s.contains("http://")) {
			json.put("website", s);
			return json;
		}
		// �ж��Ƿ�Ϊ��ͨ�ַ�
		else {
			json.put("string", s);
			return json;
		}
	}
}
